﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using AMM.Business;
using AMM.Business.Store;
using AMM.CustomClassLibrary;
using AMM.CustomWinComponents.Validator;
using AMM.CustomWinControls;
using AMM.Model;
using AMM.Model.Store;

namespace AMM.Forms.Store {
    public partial class FrmStoreBasis : FrmStoreBasisBase {

        private StoreBasisProcessor _storeBasiProcessor;
        private List<StoreBasisViewModel> _storeBasiList;

        #region Override

        protected override string FunctionCode {
            get { return "AMM1103"; }
        }

        protected override void Add() {
            this.ResetForms();
            this.FBS.CurrentItem = new StoreBasi() { IsEnabled = true, ClassStatus = ClassStatus.Added };

            this.gbStoreBasisInfo.Enabled = true;
            this.validator.ClearAllControlErrorIcons();
            base.Add();
        }

        protected override void Amend() {
            List<StoreBasisViewModel> selectedList = this.dgvStoreBasisList.GetSelectedRows<StoreBasisViewModel>();
            if (selectedList.Count > 1) { this.OnlySelectSingleRecord(); return; }
            if (selectedList.Count == 0) { this.ShouldSelectSingleRecord(); return; }
            StoreBasi storeBasi = this._storeBasiProcessor.FetchStoreBasisById(selectedList[0].Key);
            storeBasi.ClassStatus = ClassStatus.Updated;
            this.FBS.CurrentItem = storeBasi;
            this.gbStoreBasisInfo.Enabled = true;
            base.Amend();
        }

        protected override void Save() {
            base.Save();
            this.CreateNewThread(() => {
                this.PreventCancel = true;
                StoreBasi storeBasi = this.FBS.CurrentItem;

                if (storeBasi.ClassStatus == ClassStatus.Added) {
                    storeBasi.CreatedBy = LoginUserName;
                    this._storeBasiProcessor.Insert(storeBasi);
                }
                else {
                    storeBasi.ModifiedBy = LoginUserName;
                    this._storeBasiProcessor.Update(storeBasi);
                }

                this._storeBasiList = this._storeBasiProcessor.FetchStoreBasis();
                this.MDIParent.SetStatusInformation("保存成功");
            }, () => {
                this.ResetForms();
                this.PreventCancel = false;
            });
        }

        #endregion

        public FrmStoreBasis() {
            InitializeComponent();

            this.dgvStoreBasisList.DataGridViewModelType = typeof(StoreBasisViewModel);
        }

        #region Private methods

        private void ResetForms() {
            this.BindStoreBasisList();
            this.FBS.CurrentItem = null;
            this.gbStoreBasisInfo.Enabled = false;
        }

        private void CreateFormBindings() {
            this.FBS.CreateBinding(this.tbCode, x => x.Text, y => y.Code);
            this.FBS.CreateBinding(this.tbName, x => x.Text, y => y.Name);
            this.FBS.CreateBinding(this.tbDescription, x => x.Text, y => y.Description);
            this.FBS.CreateBinding(this.cbIsEnabled, x => x.Checked, y => y.IsEnabled);
            this.FBS.CreateBinding(this.cmbStoreType, x => x.SelectedValue, y => y.StoreType);
            this.FBS.CreateBinding(this.tbAreaRemarks, x => x.Text, y => y.AreaRemarks);
        }

        private void BindStoreBasisList() {
            this.dgvStoreBasisList.DataSource = new SortableBindingList<StoreBasisViewModel>(this._storeBasiList);
        }

        private void BindValidation() {
            this.validator.SetType(this.tbCode, ValidationType.Required | ValidationType.RegularExpression);
            this.validator.SetRequiredMessage(this.tbCode, this.RequiredMessage);
            this.validator.SetRegularExpression(this.tbCode, "^[A-Za-z0-9-]+$");
            this.validator.SetRegularExpressionMessage(this.tbCode, "编码只能由字母，数字，连接线组成");

            this.validator.SetType(this.tbName, ValidationType.Required);
            this.validator.SetRequiredMessage(this.tbName, this.RequiredMessage);

            this.validator.SetType(this.cmbStoreType, ValidationType.Required);
            this.validator.SetRequiredMessage(this.cmbStoreType, this.RequiredMessage);
        }

        #endregion

        private void FrmStoreBasis_Load(object sender, EventArgs e) {
            this.CreateNewThread(() => {
                this.PreventCancel = true;
                this.FBS = new FlexBindingSource<StoreBasi>();

                this._storeBasiProcessor = new StoreBasisProcessor();
                this._storeBasiList = this._storeBasiProcessor.FetchStoreBasis();
            }, () => {
                this.WindowState = FormWindowState.Maximized;

                this.BindBasics(this.cmbStoreType, BasicsType.StoreType);
                this.BindStoreBasisList();
                this.CreateFormBindings();
                this.BindValidation();
                this.ResetForms();
                this.PreventCancel = false;
            });
        }

    }
}
